﻿using GameCreator.Core;
using Gp.Scripts.Core;
using QFramework;
using UnityEngine;

namespace Gp.Scripts.GameLogic {
    // public interface IDynamicObject {
    //     string SaveKey { get; }
    // }
    
    
    public abstract class BaseDynamicObject : GlobalID {
        public virtual string SaveKey => GetID();
        protected EntityModel Model => MainArchitecture.Interface.GetModel<EntityModel>();


        protected sealed override void Awake() {
            base.Awake();
        }



        public void Init() {
            if (Model.IsObjectDestroyed(SaveKey)) {
                gameObject.DestroySelfGracefully();
                return;
            }
            
            
            OnInit();
        }

        
        protected virtual void OnInit() { }


        private void Update() {
            if (!Application.isPlaying) return;
            OnUpdate();
        }


        protected virtual void OnUpdate() { }

        protected virtual void OnDestroy() {
            // EntityModel.DynamicObjects.Remove(this);
            // if (!SceneManageSystem.SceneLoadingFlag) {
            //     EntityModel.DestroyedObjects.Add(SaveKey);
            // }
        }
    }



    public abstract class TransDynamicObject : BaseDynamicObject {
        public virtual bool IsActive => gameObject != null && gameObject.activeInHierarchy;
        
        protected override void OnInit() {
            var saveData = Model.GetInfoData(SaveKey);
            if (saveData == null) return;

            transform.position = saveData.trans.position;
            transform.rotation = saveData.trans.rotation;
            gameObject.SetActive(saveData.isActive);
        }
    }
}